package cn.edu.scau.cmi.xiaozecheng.singleton;

import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class Controller {
	@FXML
	private TextField singleInput;
	@FXML
	private TextField multiInput;
	@FXML
	private TextArea result;
	private int singleCount = 0;

	/**
	 * 点击第一个创建按钮的操作
	 */
	public void single() {
		String name = singleInput.getText().trim();
		singleInput.clear();
		if (name.length() < 1) {
			result.appendText("请输入\n");
			return;
		}
		Chairman chairman = Chairman.getInstance(name);
		result.appendText(chairman.getName() + "  " + chairman);
		if (singleCount == 0) {
			singleCount++;
			result.appendText(" 创建成功");
		} else {
			result.appendText(" 创建新对象失败,获取已有对象");
		}
		result.appendText("\n");
	}

	/**
	 * 点击第二个创建按钮的操作
	 */
	public void multi() {
		String name = multiInput.getText().trim();
		multiInput.clear();
		if (name.length() < 1) {
			result.appendText("请输入\n");
			return;
		}
		boolean state = Marshal.addInstance(name);
		if (state) {
			result.appendText(name + "元帅创建成功\n");
		} else {
			if (Marshal.getMarshals().size() == 10) {
				result.appendText("已经有10个人了,"+name+"创建失败\n");
			} else {
				result.appendText(name + "元帅已经存在\n");
			}
		}
		result.appendText("当前存在的元帅有:" + Marshal.getMarshals() + "\n");
	}

	/**
	 * 清空日志
	 */
	public void clearText() {
		result.clear();
	}

	/**
	 * 重新来过
	 */
	public void onceAgain() {
		Chairman.clearInstance();
		singleCount = 0;
		Marshal.clearMarshals();
	}

}
